package com.kltn.zigo;

import java.util.ArrayList;
import java.util.HashMap;

import com.kltn.zigo.ListPlaceActivity.LoadPlaceAsync;
import com.kltn.zigo.data.adapter.PlaceAdapter;
import com.kltn.zigo.data.adapter.ProvinceAdapter;
import com.kltn.zigo.data.adapter.UserAdapter;
import com.kltn.zigo.data.loader.ListUserLoader;
import com.kltn.zigo.data.loader.PlaceLoader;
import com.kltn.zigo.process.JSONParser;

import android.os.AsyncTask;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ListUserActivity extends Activity {
	
	JSONParser jParser = new JSONParser();
    String provinceID = "1";

	// Progress Dialog
	private ProgressDialog pDialog;

    private ArrayList<HashMap<String, String>> data;
    
	ListView list;
	EditText inputSearch;
	UserAdapter adapter;	
    private static LayoutInflater inflater=null;
    String userIdClicked = "";
    String usernameClicked = "";
    String userdisplaynameClicked = "";
    String useravatarClicked = "";
    
    String userid;
	String username;
	String userdisplayname;
	String useravatarurl;
	
    private MenuItem btnMenu;
    private MenuItem btnSearch;
    private MenuItem btnProfile;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_user);
		
		ActionBar actionBar = getActionBar();		
		actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#b51d21")));		    
        
		userid = getIntent().getStringExtra("userid");
		username = getIntent().getStringExtra("username");
		userdisplayname = getIntent().getStringExtra("userdisplayname");
		useravatarurl = getIntent().getStringExtra("useravatarurl");
		
		list=(ListView)findViewById(R.id.list_user);
		
		 new LoadListUser().execute();
	        
	        
	      list.setOnItemClickListener(new OnItemClickListener() {
	        	@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {							
	        		View vi=view;
			        if(view==null)
			            vi = inflater.inflate(R.layout.item_list_place, null);

			        TextView userID = (TextView)vi.findViewById(R.id.tvID);
			        TextView usernametv = (TextView)vi.findViewById(R.id.tvUsername);
			        TextView displayname = (TextView)vi.findViewById(R.id.tvDisplayname);
			        TextView urlavatar = (TextView)vi.findViewById(R.id.tvUrlAvatar);
			        
			        
			        userIdClicked = userID.getText().toString();
			        usernameClicked = usernametv.getText().toString();
			        userdisplaynameClicked = displayname.getText().toString();
			        useravatarClicked = urlavatar.getText().toString();
			        
			        Intent i=new Intent(ListUserActivity.this,ProfileUserActivity.class);
			        i.putExtra("userid", userid);
			        i.putExtra("username", username);
			        i.putExtra("userdisplayname", userdisplayname);
			        i.putExtra("useravatarurl", useravatarurl);
			        
			        i.putExtra("useridView",userIdClicked);
			        i.putExtra("usernameView", usernameClicked);
			        i.putExtra("userdisplaynameView",userdisplaynameClicked);
			        i.putExtra("useravatarurlView", useravatarClicked);
				    startActivity(i);
				}
			});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.list_user, menu);
		
		btnMenu = menu.findItem(R.id.btn_menu);
		btnSearch = menu.findItem(R.id.btn_search);
		btnProfile = menu.findItem(R.id.btn_profile);
		
		return true;
	}
	
	@Override
	  public boolean onOptionsItemSelected(MenuItem item) {
		 switch (item.getItemId()) {
		   
		    case R.id.btn_profile:
		    {
		    	Intent i=new Intent(getApplicationContext(),ProfileUserActivity.class);
		        i.putExtra("userid", userid);
		        i.putExtra("username", username);
		        i.putExtra("userdisplayname", userdisplayname);
		        i.putExtra("useravatarurl", useravatarurl);
		        
		        i.putExtra("useridView","");
		        i.putExtra("usernameView", "");
		        i.putExtra("userdisplaynameView","");
		        i.putExtra("useravatarurlView", "");
			    startActivity(i);
		    	break;
		    }
		    	
		    case R.id.btn_search:
		    {
		    	Intent i=new Intent(getApplicationContext(),ListPlaceActivity.class);
		    	i.putExtra("userid",userid);
		        i.putExtra("username", username);
		        i.putExtra("userdisplayname", userdisplayname);
		        i.putExtra("useravatarurl", useravatarurl);
			    startActivity(i);
			    break;
		    }
		    
		    case R.id.btn_menu:
		    {
		    	Intent i=new Intent(getApplicationContext(),MainActivity.class);
		        i.putExtra("userid", userid);
		        i.putExtra("username", username);
		        i.putExtra("userdisplayname", userdisplayname);
		        i.putExtra("useravatarurl", useravatarurl);
			    startActivity(i);
		    	break;
		    }
		 }
		 return super.onOptionsItemSelected(item);
	  }
	
	class LoadListUser extends AsyncTask<String, String, String>
    {
    	ListUserLoader listuserload = new ListUserLoader();
    	@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(ListUserActivity.this);
			pDialog.setMessage("Loading users. Please wait...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(false);
			pDialog.show();
		}
    	
		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
	    	//placeload = new PlaceLoader();
			
			data = listuserload.GetUser();
			return null;
		}
		
		protected void onPostExecute(String file_url) {
			// dismiss the dialog after getting all products
			pDialog.dismiss();
			// updating UI from Background Thread
			runOnUiThread(new Runnable() {
				public void run() {
					adapter = new UserAdapter(ListUserActivity.this, listuserload, data);
					//adapter = new PlaceAdapter(ListPlaceActivity.this, placeload);
			        list.setAdapter(adapter);
			        
			        inputSearch = (EditText) findViewById(R.id.inputSearch);

                    inputSearch.addTextChangedListener(new TextWatcher() {

                        @Override
                        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                            // When user changed the Text
                            ListUserActivity.this.adapter.getFilter().filter(cs);   
                        }

                        @Override
                        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                int arg3) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void afterTextChanged(Editable arg0) {
                            // TODO Auto-generated method stub                          
                        }
                    });
				}
			});

		}
    }

}
